package com.uatair.system.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uatair.common.mqtt.domain.RegionData;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Service
public class RegionService {
    private RegionData regionData;

    @PostConstruct  // 在 Bean 初始化时加载数据
    public void init() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 从 resources/json/region.json 读取
            InputStream inputStream = new ClassPathResource("json/region.json").getInputStream();
            regionData = objectMapper.readValue(inputStream, RegionData.class);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load region data", e);
        }
    }

    // 获取省份列表
    public Map<String, String> getProvinceList() {
        return regionData.getProvinceList();
    }

    // 获取城市列表
    public Map<String, String> getCityList() {
        return regionData.getCityList();
    }

    // 根据省份编码获取名称
    public String getProvinceName(String code) {
        return regionData.getProvinceList().get(code);
    }

    // 根据城市编码获取名称
    public String getCityName(String code) {
        return regionData.getCityList().get(code);
    }
}
